﻿// OptionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "iTrans.h"
#include "OptionDlg.h"
#include "afxdialogex.h"
#include "SmallUtilz.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// COptionDlg dialog

IMPLEMENT_DYNAMIC(COptionDlg, CDialog)

COptionDlg::COptionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COptionDlg::IDD, pParent)
	, m_b4GBonly(FALSE)
	, m_iConversionMode(2)
	, m_iConversionProfile(2)
	, m_iIDevice(2)
	, m_bCheck30fps(TRUE)
	, m_bKeepPrefixOnlyWhenNeeded(FALSE)
	, m_iPAR(2)
	, m_bDeinterlace(TRUE)
	, m_bKeepRotate(FALSE)
	, m_iRotate(0)
	, m_bPathFirst(TRUE)
	, m_bCopyOriginDate(FALSE)
	, m_bIgnoreUTC(TRUE)
	, m_bFfmpegThreads(FALSE)
	, m_uFfmpegThreads(0)
	, m_bBothAudio(FALSE)
	, m_bForceReEncodeAAC(FALSE)
	, m_bAudioOrder(FALSE)
	, m_iAudioOrder(0)
	, m_iAudioBR(0)
	, m_bNormalizeAudio(FALSE)
	, m_dNormalizeAudioInDB(-0.25)
	, m_bDontDownmixAudio(FALSE)
	, m_bMuxAC3(FALSE)
	, m_bExtract2SRT(FALSE)
	, m_iFontSize(0)
	, m_bBothSubtitles(FALSE)
	, m_bSrtLang(FALSE)
	, m_iSrtLang(0)
	, m_bBurnSubtitle(FALSE)
	, m_bDefaultLangA(FALSE)
	, m_iDefaultLangA(0)
	, m_bDefaultLangS(FALSE)
	, m_iDefaultLangS(0)
	, m_iKeepTempFiles(0)
	, m_bUseSaveFolder(FALSE)
	, lpRegKey(NULL)
{
}

COptionDlg::~COptionDlg()
{
}

void COptionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_CBIndex(pDX, IDC_COMBO1, m_iIDevice);
	DDX_CBIndex(pDX, IDC_COMBO2, m_iConversionMode);
	DDX_CBIndex(pDX, IDC_COMBO3, m_iConversionProfile);
	DDX_CBIndex(pDX, IDC_COMBO4, m_iRotate);
	DDX_CBIndex(pDX, IDC_COMBO5, m_iPAR);
	DDX_CBIndex(pDX, IDC_COMBO7, m_iAudioOrder);
	DDX_CBIndex(pDX, IDC_COMBO8, m_iAudioBR);
	DDX_CBIndex(pDX, IDC_COMBO9, m_iFontSize);
	DDX_CBIndex(pDX, IDC_COMBO10, m_iSrtLang);
	DDX_CBIndex(pDX, IDC_COMBO11, m_iDefaultLangA);
	DDX_CBIndex(pDX, IDC_COMBO12, m_iDefaultLangS);
	DDX_CBIndex(pDX, IDC_COMBO13, m_iKeepTempFiles);
	DDX_Text(pDX, IDC_EDIT1, m_uFfmpegThreads);
	DDX_Text(pDX, IDC_EDIT2, m_dNormalizeAudioInDB);
	DDX_Check(pDX, IDC_CHECK1, m_bDeinterlace);
	DDX_Check(pDX, IDC_CHECK2, m_bKeepRotate);
	DDX_Check(pDX, IDC_CHECK3, m_bKeepPrefixOnlyWhenNeeded);
	DDX_Check(pDX, IDC_CHECK4, m_bCopyOriginDate);
	DDX_Check(pDX, IDC_CHECK5, m_b4GBonly);
	DDX_Check(pDX, IDC_CHECK6, m_bCheck30fps);
	DDX_Check(pDX, IDC_CHECK7, m_bFfmpegThreads);
	DDX_Check(pDX, IDC_CHECK21, m_bBothAudio);
	DDX_Check(pDX, IDC_CHECK20, m_bForceReEncodeAAC);
	DDX_Check(pDX, IDC_CHECK8, m_bAudioOrder);
	DDX_Check(pDX, IDC_CHECK9, m_bNormalizeAudio);
	DDX_Check(pDX, IDC_CHECK10, m_bDontDownmixAudio);
	DDX_Check(pDX, IDC_CHECK11, m_bMuxAC3);
	DDX_Check(pDX, IDC_CHECK12, m_bExtract2SRT);
	DDX_Check(pDX, IDC_CHECK13, m_bBurnSubtitle);
	DDX_Check(pDX, IDC_CHECK14, m_bBothSubtitles);
	DDX_Check(pDX, IDC_CHECK15, m_bSrtLang);
	DDX_Check(pDX, IDC_CHECK16, m_bDefaultLangA);
	DDX_Check(pDX, IDC_CHECK17, m_bDefaultLangS);
	DDX_Check(pDX, IDC_CHECK18, m_bUseSaveFolder);
	DDX_Check(pDX, IDC_CHECK19, m_bPathFirst);
	DDX_Check(pDX, IDC_CHECK22, m_bIgnoreUTC);
}


BEGIN_MESSAGE_MAP(COptionDlg, CDialog)
	ON_WM_HELPINFO()
	ON_CBN_SELCHANGE(IDC_COMBO1, &COptionDlg::OnSelchangeComboMachine)
	ON_CBN_SELCHANGE(IDC_COMBO2, &COptionDlg::OnSelchangeConversionMode)
	ON_BN_CLICKED(IDC_CHECK7, &COptionDlg::OnClickedCheckFfmpegThreads)
	ON_BN_CLICKED(IDC_CHECK8, &COptionDlg::OnClickedCheckChooseAudio)
	ON_BN_CLICKED(IDC_CHECK9, &COptionDlg::OnClickedCheckNorm)
	ON_BN_CLICKED(IDC_CHECK13, &COptionDlg::OnClickedCheckBurnSubtitle)
	ON_BN_CLICKED(IDC_CHECK15, &COptionDlg::OnClickedCheckSRTLangCode)
	ON_BN_CLICKED(IDC_CHECK16, &COptionDlg::OnClickedCheckDefaultLangA)
	ON_BN_CLICKED(IDC_CHECK17, &COptionDlg::OnClickedCheckDefaultLangS)
	ON_BN_CLICKED(IDC_CHECK18, &COptionDlg::OnClickedCheckUseSaveFolder)
	ON_BN_CLICKED(IDC_BUTTON1, &COptionDlg::OnClickedButtonSetTempFolder)
	ON_BN_CLICKED(IDC_BUTTON2, &COptionDlg::OnClickedButtonSetSaveFolder)
	ON_BN_CLICKED(IDC_BUTTON3, &COptionDlg::OnClickedButtonSaveToRegistry)
	ON_BN_CLICKED(IDC_BUTTON4, &COptionDlg::OnClickedButtonInitValues)
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipNotify)
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipNotify)
	ON_BN_CLICKED(IDC_CHECK4, &COptionDlg::OnClickedCheckCopyFileDate)
END_MESSAGE_MAP()

// COptionDlg message handlers


int CALLBACK BrowseCallbackProc(HWND hwnd,
	UINT uMsg,
	LPARAM lParam,
	LPARAM lpData)
{
	switch (uMsg) {
	case BFFM_INITIALIZED :
		{
			// 해당 Control ID는 shell32.dll version 5.0 이상에서 사용된다.
			// 하위 버전에서는 현재 tree control이 id가 다르며
			// 새폴더 버튼을 생성할 수 없다.
			HWND hShell = GetDlgItem(hwnd, 0);  // 0x00000000(Shell Class)
			HWND hTree = GetDlgItem(hShell, 100); // 0x00000064(Tree Control)
			HWND hNew = GetDlgItem(hwnd, 14150); // 0x00003746(New Folder Button)
			HWND hOK = GetDlgItem(hwnd, 1);  // 0x00000001(OK Button)
			HWND hCancel= GetDlgItem(hwnd, 2);  // 0x00000002(Cancel Button)
			HWND hStatic= GetDlgItem(hwnd, 14146); // 0x00003742(Static Control)

			// 하나라도 못가져오면 기본 구성으로 처리한다.
			if (!hShell || !hTree || !hNew || !hOK || !hCancel)
				return 0;

			// 상단 표시줄 삭제
			if (hStatic) {
				EnableWindow(hStatic, FALSE);
				ShowWindow(hStatic, SW_HIDE);
			}

			CRect rectWnd;
			CRect rectNew;
			CRect rectOK;
			CRect rectCancel;
			GetClientRect(hwnd, &rectWnd);
			GetClientRect(hNew, &rectNew);
			GetClientRect(hOK, &rectOK);
			GetClientRect(hCancel, &rectCancel);

			// Tree 크기조정
			// Tree를 가지는 shell의 크기조정
			MoveWindow(hShell,
				rectWnd.left+10,
				rectWnd.top+10,
				rectWnd.Width()-22, // 20이지만 실제로 보면 작아 보인다. 따라서 2를 추가로 빼준다.
				rectWnd.Height()-rectNew.Height()-30,
				TRUE);
			// 새 폴더 Button 변경
			MoveWindow(hNew,
				rectWnd.left+10,
				rectWnd.bottom-rectNew.Height()-10,
				rectNew.Width(),
				rectNew.Height(),
				TRUE);
			// 확인 Button
			MoveWindow(hOK,
				rectWnd.right-10-rectCancel.Width()-5-rectOK.Width(),
				rectWnd.bottom-rectOK.Height()-10,
				rectOK.Width(),
				rectOK.Height(),
				TRUE);
			// 취소 Button
			MoveWindow(hCancel,
				rectWnd.right-10-rectCancel.Width(),
				rectWnd.bottom-rectCancel.Height()-10,
				rectCancel.Width(),
				rectCancel.Height(),
				TRUE);
			// 첫 폴더
			::SendMessage( hwnd, BFFM_SETSELECTION, TRUE, lpData );
		}
		break;
	case BFFM_SELCHANGED :
	case BFFM_VALIDATEFAILED :
		break;
	}
	return 0;
}

CString SelectFolder(HWND hWnd, TCHAR* pszInitPath)
{
	CString strFolder;
	ITEMIDLIST* pidlBrowse;
	TCHAR  achPath[MAX_PATH];
	BROWSEINFO brInfo;

	if (pszInitPath && *pszInitPath)
		_tcscpy_s(achPath, MAX_PATH, pszInitPath);

	ZeroMemory(&brInfo, sizeof(BROWSEINFO));

	brInfo.hwndOwner  = hWnd;
	brInfo.pidlRoot   = NULL;
	brInfo.pszDisplayName = achPath;
	brInfo.lpszTitle  = _T("");
	brInfo.ulFlags   = BIF_NEWDIALOGSTYLE|BIF_RETURNONLYFSDIRS|BIF_DONTGOBELOWDOMAIN;
	brInfo.lpfn    = BrowseCallbackProc;
	brInfo.lParam = (LPARAM)achPath;
	pidlBrowse    = ::SHBrowseForFolder( &brInfo );

	if(pidlBrowse != NULL) {
		SHGetPathFromIDList( pidlBrowse, achPath );

		// pszPathname에 선택한 폴더가 들어있습니다.
		strFolder = achPath;
	}

	return strFolder;
}

BOOL COptionDlg::OnHelpInfo(HELPINFO* pHelpInfo)
{
	return TRUE;
	//return CDialog::OnHelpInfo(pHelpInfo);
}

BOOL COptionDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	applyEnableStatus();
	EnableToolTips();

	return TRUE;
}

void COptionDlg::enableControlArray(UINT *idList, int count, BOOL bEnabled)
{
	for (int i=0; i<count; i++) {
		CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, idList[i], bEnabled);
	}
}

void COptionDlg::OnSelchangeComboMachine()
{
	UpdateData();

	//CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO2, m_iIDevice != 3);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO3, (m_iConversionMode>1));
	//CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO6, m_iIDevice != 3);

	static UINT dlgItems[] = {
		IDC_CHECK2,		IDC_COMBO4,		IDC_CHECK13
	};
	BOOL bBurnSubEnabled = (m_iConversionMode==3);
	enableControlArray(dlgItems, sizeof(dlgItems)/sizeof(dlgItems[0]), bBurnSubEnabled);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK1, (m_iConversionMode>=2));

	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK17, !(bBurnSubEnabled && m_bBurnSubtitle));
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO12, !(bBurnSubEnabled && m_bBurnSubtitle) && m_bDefaultLangS);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK14, !(bBurnSubEnabled && m_bBurnSubtitle));
}


void COptionDlg::OnSelchangeConversionMode()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO3, m_iConversionMode>1);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK6, m_iConversionMode);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO1, m_iConversionMode);

	static UINT dlgItems[] = {
		IDC_CHECK2,		IDC_COMBO4,		IDC_CHECK13
	};
	BOOL bBurnSubEnabled = (m_iConversionMode==3);
	enableControlArray(dlgItems, sizeof(dlgItems)/sizeof(dlgItems[0]), bBurnSubEnabled);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK1, (m_iConversionMode>=2));

	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK17, !(bBurnSubEnabled && m_bBurnSubtitle));
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO12, !(bBurnSubEnabled && m_bBurnSubtitle) && m_bDefaultLangS);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK14, !(bBurnSubEnabled && m_bBurnSubtitle));
}


void COptionDlg::OnClickedCheckFfmpegThreads()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_EDIT1, m_bFfmpegThreads);
}

void COptionDlg::OnClickedCheckChooseAudio()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO7, m_bAudioOrder);
}

void COptionDlg::OnClickedCheckNorm()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_EDIT2, m_bNormalizeAudio);
}

void COptionDlg::OnClickedCheckBurnSubtitle()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK17, !m_bBurnSubtitle);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO12, !m_bBurnSubtitle && m_bDefaultLangS);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK14, !m_bBurnSubtitle);
}

void COptionDlg::OnClickedCheckSRTLangCode()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO10, m_bSrtLang);
}

void COptionDlg::OnClickedCheckDefaultLangA()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO11, m_bDefaultLangA);
}

void COptionDlg::OnClickedCheckDefaultLangS()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO12, m_bDefaultLangS);
}

void COptionDlg::OnClickedCheckUseSaveFolder()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_BUTTON2, m_bUseSaveFolder);
}

void COptionDlg::applyEnableStatus()
{
	static UINT nIDs0[] = {
		IDC_BUTTON1,	IDC_BUTTON4,	IDOK,			IDCANCEL,
		IDC_CHECK4,		IDC_CHECK5,		IDC_CHECK8,		IDC_CHECK9,
		IDC_CHECK10,	IDC_CHECK15,	IDC_CHECK16,	IDC_CHECK18,
		IDC_CHECK19,	
		IDC_COMBO5,		IDC_COMBO8,		IDC_COMBO9,		IDC_COMBO13,
		IDC_STATIC_SUB
	};
	enableControlArray(nIDs0, sizeof(nIDs0)/sizeof(nIDs0[0]));

	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO7, m_bAudioOrder);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_BUTTON2, m_bUseSaveFolder);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO10, m_bSrtLang);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK22, m_bCopyOriginDate);

	static UINT nIDs1[] = {
		IDC_CHECK2,		IDC_COMBO4,		IDC_CHECK13
	};
	BOOL bBurnSubEnabled = (m_iConversionMode==3);
	enableControlArray(nIDs1, sizeof(nIDs1)/sizeof(nIDs1[0]), bBurnSubEnabled);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK1, (m_iConversionMode>=2));

	static UINT nIDs2[] = {
		IDC_COMBO1,		IDC_CHECK6
	};
	enableControlArray(nIDs2, sizeof(nIDs2)/sizeof(nIDs2[0]), m_iConversionMode);

	static UINT nIDs3[] = {
		IDC_COMBO2,		IDC_COMBO6
	};
	//enableControlArray(nIDs3, sizeof(nIDs3)/sizeof(nIDs3[0]), m_iIDevice != 3);

	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO3, (m_iConversionMode>1));

	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO11, m_bDefaultLangA);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK17, !(bBurnSubEnabled && m_bBurnSubtitle));
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_COMBO12, !(bBurnSubEnabled && m_bBurnSubtitle) && m_bDefaultLangS);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK14, !(bBurnSubEnabled && m_bBurnSubtitle));
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_EDIT2, m_bNormalizeAudio);
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_EDIT1, m_bFfmpegThreads);
}

void COptionDlg::OnClickedButtonInitValues()
{
	m_b4GBonly = FALSE;
	m_iConversionMode = 2;
	m_iConversionProfile = 2;
	m_iIDevice = 2;
	m_bCheck30fps = TRUE;
	m_bKeepPrefixOnlyWhenNeeded = FALSE;
	m_iPAR = 2;
	m_bDeinterlace = TRUE;
	m_bKeepRotate = FALSE;
	m_iRotate = 0;
	m_bPathFirst = TRUE;
	m_bCopyOriginDate = FALSE;
	m_bIgnoreUTC = TRUE;
	m_bFfmpegThreads = FALSE;
	m_uFfmpegThreads = 0;
	m_bBothAudio = FALSE;
	m_bForceReEncodeAAC = 0;
	m_bAudioOrder = FALSE;
	m_iAudioOrder = 0;
	m_iAudioBR = 0;
	m_bNormalizeAudio = FALSE;
	m_dNormalizeAudioInDB = -0.25;
	m_bDontDownmixAudio = FALSE;
	m_bMuxAC3 = FALSE;
	m_bExtract2SRT = FALSE;
	m_iFontSize = 0;
	m_bBothSubtitles = FALSE;
	m_bSrtLang = FALSE;
	m_iSrtLang = 0;
	m_bBurnSubtitle = FALSE;
	m_bDefaultLangA = FALSE;
	m_iDefaultLangA = 0;
	m_bDefaultLangS = FALSE;
	m_iDefaultLangS = 0;
	m_iKeepTempFiles = 0;
	m_bUseSaveFolder = FALSE;

	UpdateData(FALSE);

	applyEnableStatus();

	removeHKCUKey(lpRegKey);

	MessageBox(_T("옵션 초기화 및 레지스트리 삭제 완료"));
}

void COptionDlg::OnClickedButtonSaveToRegistry() 
{
	SaveToRegistry(true);

	MessageBox(_T("옵션 저장 완료"));
}

void COptionDlg::OnClickedButtonSetTempFolder() 
{
	CString strFolderPath = SelectFolder(this->GetSafeHwnd(), LPTSTR(LPCTSTR(csTempPath)));
	if (strFolderPath.GetLength()) {
		csTempPath = strFolderPath;

		CSmallUtilz4iTrans::CheckTempFolder(csTempPath, m_iKeepTempFiles==1);

		csTempPath.TrimRight(_TCHAR('\\'));
		if (csTempPath.GetLength()) csTempPath+=_TCHAR('\\');
	}
}

void COptionDlg::OnClickedButtonSetSaveFolder() 
{
	CString strFolderPath = SelectFolder(this->GetSafeHwnd(), LPTSTR(LPCTSTR(csSavePath)));
	if (strFolderPath.GetLength()) {
		csSavePath = strFolderPath;
		csSavePath.TrimRight(_TCHAR('\\'));
		csSavePath+=_TCHAR('\\');
	}
}

void COptionDlg::SaveToRegistry(bool bUpdateData)
{
	//HKEY_CURRENT_USER\Software\BLUEnLIVE\iTrans
	if (bUpdateData) {
		UpdateData();
	}

	removeHKCUKey(lpRegKey);

	HKEY hkey;
	if (RegCreateKey(HKEY_CURRENT_USER, lpRegKey, &hkey) == ERROR_SUCCESS) {
		dwRegValues[0] = DWORD(m_b4GBonly);
		dwRegValues[1] = DWORD(m_iConversionMode);
		dwRegValues[2] = DWORD(m_iConversionProfile);
		dwRegValues[3] = DWORD(m_iIDevice);
		dwRegValues[4] = DWORD(m_bCheck30fps);
		dwRegValues[5] = DWORD(m_bKeepPrefixOnlyWhenNeeded);
		dwRegValues[6] = DWORD(m_iPAR);
		dwRegValues[7] = DWORD(m_bDeinterlace);
		dwRegValues[8] = DWORD(m_bKeepRotate);
		dwRegValues[9] = DWORD(m_iRotate);
		dwRegValues[10] = DWORD(m_bPathFirst);
		dwRegValues[11] = DWORD(m_bCopyOriginDate);
		dwRegValues[12] = DWORD(m_bIgnoreUTC);
		dwRegValues[13] = DWORD(m_bFfmpegThreads);
		dwRegValues[14] = DWORD(m_uFfmpegThreads);
		dwRegValues[15] = DWORD(m_bBothAudio);
		dwRegValues[16] = DWORD(m_bForceReEncodeAAC);
		dwRegValues[17] = DWORD(m_bAudioOrder);
		dwRegValues[18] = DWORD(m_iAudioOrder);
		dwRegValues[19] = DWORD(m_iAudioBR);
		dwRegValues[20] = DWORD(m_bNormalizeAudio);
		dwRegValues[21] = CSmallUtilz4iTrans::decibel2DWORD(m_dNormalizeAudioInDB);
		dwRegValues[22] = DWORD(m_bDontDownmixAudio);
		dwRegValues[23] = DWORD(m_bMuxAC3);
		dwRegValues[24] = DWORD(m_bExtract2SRT);
		dwRegValues[25] = DWORD(m_iFontSize);
		dwRegValues[26] = DWORD(m_bBothSubtitles);
		dwRegValues[27] = DWORD(m_bSrtLang);
		dwRegValues[28] = DWORD(m_iSrtLang);
		dwRegValues[29] = DWORD(m_bBurnSubtitle);
		dwRegValues[30] = DWORD(m_bDefaultLangA);
		dwRegValues[31] = DWORD(m_iDefaultLangA);
		dwRegValues[32] = DWORD(m_bDefaultLangS);
		dwRegValues[33] = DWORD(m_iDefaultLangS);
		dwRegValues[34] = DWORD(m_iKeepTempFiles);
		dwRegValues[35] = DWORD(m_bUseSaveFolder);

		// 레지스트리에는
		// 0:Slow .. 4:Ultra fast, 5:HiBR+Very slow, 6:Default 로 저장하지만
		// 드롭다운의 값은
		// 0:Default, 1:Slow .. 5:Ultra fast, 6:HiBR+Very slow
		// -> 이건 구버전 구성임
		// m_iConversionProfile2는 있는 그대로 저장

		for (int i=0; i<36; i++) {
			SetRegValueDWORD(hkey, lpRegNames[i], dwRegValues[i]);
		}

		//작업 폴더 관련 옵션만 따로 처리
		SetRegValueTCHAR(hkey, lpRegNames[36], const_cast<LPTSTR>(static_cast<LPCTSTR>(csTempPath)));
		SetRegValueTCHAR(hkey, lpRegNames[37], const_cast<LPTSTR>(static_cast<LPCTSTR>(csSavePath)));

		RegCloseKey(hkey);
	}
}

void COptionDlg::SetRegValueTCHAR(HKEY hkey, LPCTSTR tcName, TCHAR *tcData)
{
	RegSetValueEx(hkey, tcName, 0, REG_SZ, reinterpret_cast<LPBYTE>(tcData), (_tcslen(tcData)+1)*sizeof(TCHAR));
}

void COptionDlg::SetRegValueDWORD(HKEY hkey, LPCTSTR tcName, DWORD dwNum)
{
	RegSetValueEx(hkey, tcName, 0, REG_DWORD, reinterpret_cast<LPBYTE>(&dwNum), sizeof(DWORD));
}

void COptionDlg::removeHKCUKey(LPCTSTR tcKey)
{
	if (tcKey && tcKey[0])
		SHDeleteKey(HKEY_CURRENT_USER, tcKey);
}

BOOL COptionDlg::OnToolTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
	// Get the tooltip structure.
	TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;

	// Check once again that the idFrom holds handle itself.
	if (pTTT->uFlags & TTF_IDISHWND) {
		// Get the control's ID.
		UINT nID = ::GetDlgCtrlID((HWND)pNMHDR->idFrom);

		if (!nID) return FALSE;

		// Now you have the ID. depends on control,
		// set your tooltip message.

		static UINT nIDs[] = {
			IDC_CHECK5,
			IDC_COMBO2,
			IDC_COMBO3,
			IDC_COMBO1,
			IDC_CHECK6,
			IDC_CHECK3,
			IDC_COMBO5,
			IDC_CHECK1,
			IDC_CHECK2,
			IDC_COMBO4,
			IDC_CHECK19,
			IDC_CHECK4,
			IDC_CHECK22,
			IDC_CHECK7,		IDC_EDIT1,
			IDC_CHECK21,
			IDC_CHECK20,
			IDC_CHECK8,		IDC_COMBO7,
			IDC_CHECK9,		IDC_EDIT2,
			IDC_CHECK10,
			IDC_CHECK11,
			IDC_COMBO9,
			IDC_CHECK14,
			IDC_CHECK15,	IDC_COMBO10,
			IDC_CHECK13,
			IDC_CHECK16,	IDC_COMBO11,
			IDC_CHECK17,	IDC_COMBO12,
			IDC_CHECK12
		};

		static signed char scStrs[] = {
			0,
			1,
			2,
			3,
			4,
			5,
			6,
			7,
			8,
			9,
			10,
			11,
			12,
			13,	13,
			14,
			15,
			16,	16,
			17,	17,
			18,
			19,
			20,
			21,
			22,	22,
			23,
			24,	24,
			25,	25,
			26
		};

		LPTSTR lpStrs[] = {
			_T("4GB 이하의 파일만 처리"),
			_T("비디오 변환 방식을 지정.\r\n재인코딩은 H.264로만 처리함"),
			_T("H.264 재인코딩 프리셋\r\n(Default 외엔 모두 2-pass)"),
			_T("업로드 대상 기기"),
			_T("최대 프레임율을 30fps로 제한"),
			_T("파일명 앞에 prefix[i)]를 필요한 경우에만 붙일지\r\n또는 언제나 붙일지 여부 지정"),
			_T("PAR/SAR 강제변환 여부 지정.\r\n강제변환시 화면비 왜곡됨"),
			_T("재인코딩시 원본이 인터레이스가 아니어도 강제 디인터레이스"),
			_T("영상 회전시 소스 파일의 회전정보 반영"),
			_T("영상 회전 지정"),
			_T("PATH에 지정된 프로그램을 우선 검색"),
			_T("원본 파일의 날짜를 생성된 파일에 덮어씀"),
			_T("MediaInfo의 날짜/시간 정보가 UTC일 때도 local time으로 인식\r\n(체크하기를 권장함)"),
			_T("FFMpeg로 비디오 인코딩시 threads 수 지정\r\n- 0으로 지정시 전 코어를 최대한 활용\r\n- 미지정시 코어수-1"),
			_T("외부 오디오가 존재할 때 내부 오디오와 외부 오디오를 모두 변환"),
			_T("원본 오디오가 AAC라도 다시 재인코딩"),
			_T("내부 오디오 하나(국가 또는 트랙) 선택 또는 전체 오디오 선택"),
			_T("음량 정규화 여부 및 최대 음량을 dB로 지정\r\n기본값은 -0.25dB(97%)"),
			_T("멀티 채널 오디오를 2ch로 다운믹스 하지 않음\r\n(멀티채널 AAC는 iDevice에 업로드 불가)"),
			_T("원본 오디오에 AC3가 있는 경우 생성되는 파일에 AC3 오디오를 통합"),
			_T("자막을 영상에 입힐 때 외엔 무의미한 기능. iOS에서 지원하지 않음\r\n(ttxt 필드에는 있으므로 언젠간 지원할지도…)"),
			_T("외부 자막이 존재할 때 내부 자막과 외부 자막을 모두 변환"),
			_T("ANSI 포맷 srt 자막의 코드페이지 강제 지정\r\n(@oktoya님 요구사항인데, 대체 뭐하러…)"),
			_T("영상에 자막을 직접 입힘.\r\n비디오 강제 재인코딩시에만 동작하며, 자막을 별도로 저장하진 않음"),
			_T("언어를 인식하지 못한 오디오에 대해 언어 강제 지정"),
			_T("언어를 인식하지 못한 자막에 대해 언어 강제 지정"),
			_T("원본이 mkv/mp4 파일인 경우 자막을 srt 포맷으로 저장")
		};
		
		::SendMessage(pNMHDR->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 800);
		int iStrNo = -1;
		for (int i=0; i<sizeof(nIDs)/sizeof(nIDs[0]); i++) {
			if (nIDs[i] == nID) {
				iStrNo = i;
			}
		}

		if (iStrNo!=-1) {
			pTTT->lpszText = lpStrs[scStrs[iStrNo]];
			return TRUE;
		}
	}

	// Not handled.
	return FALSE;
}


void COptionDlg::OnClickedCheckCopyFileDate()
{
	UpdateData();
	CSmallUtilz4iTrans::EnableDialgItem(m_hWnd, IDC_CHECK22, m_bCopyOriginDate);
}


